package com.ruoyi.quartz.uniController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.GroupName;
import com.ruoyi.system.domain.Plaza;
import com.ruoyi.system.enumutil.PlazaEnum;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.PlazaService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： UniPlazaController
 *  创建时间：2025/2/14
 *  广场
 */
@RestController
@RequestMapping("/uni/plaza")
public class UniPlazaController {

    //广场
    @Resource
    private PlazaService plazaService;

    //用户
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     *  没有置顶排在下面
     * 置顶了的置顶时间里现在越近又要排在签名
     */
    //查询微信群名称分组
    @PostMapping("/get/plaza")
    public AjaxResult getPlaza(@RequestBody Plaza plaza){
        //创建分页对象
        Page<Plaza> page = new Page<>();
        //设置页码
        page.setCurrent(plaza.getCurrent());
        //设置行数
        page.setSize(plaza.getSize());
        //查询数据
        plazaService.page(page,new LambdaQueryWrapper<Plaza>()
                //审核状态
                .eq(Plaza::getStatus, PlazaEnum.ShenHeTongGuo.getStatus())
                //逻辑删除为false
                .eq(Plaza::getDeleteFlag,Boolean.FALSE)
                //先通过时间
                .orderByDesc(Plaza::getTopTime)
                //热度
                .orderByDesc(Plaza::getHeat)
                //排序
                .orderByDesc(Plaza::getCreateTime)
                //添加分页语句
                .last(" limit " + (plaza.getCurrent() - 1) * plaza.getSize() +"," + plaza.getSize())
        );
        //设置总行数
        page.setTotal(plazaService.count(new LambdaQueryWrapper<Plaza>()
                //审核状态
                .eq(Plaza::getStatus, PlazaEnum.ShenHeTongGuo.getStatus())
                //先通过时间
                .orderByDesc(Plaza::getTopTime)
                //热度
                .orderByDesc(Plaza::getHeat)
                //逻辑删除为false
                .eq(Plaza::getDeleteFlag,Boolean.FALSE)
        ));
        //遍历数据
        page.getRecords().stream().forEach(ele->{
            //判断用户id不为 null
            if (Objects.nonNull(ele.getUserId())){
                //查询用户
                SysUser sysUser = sysUserMapper.selectUserById(ele.getUserId());
                //判断 赋值
                if (Objects.nonNull(sysUser)) {
                    //判断用户是否未高级会员
                    if (sysUser.getMemberEndTime().after(new Date()) && Objects.nonNull(sysUser.getMemberId())){
                        //设置会员名称
                        sysUser.setMemberName("高级会员");
                    }else{
                        //设置会员名称
                        sysUser.setMemberName("普通会员");
                    };
                    //设置用户
                    ele.setSysUser(sysUser);
                }
            }
        });
        //返回结果
        return AjaxResult.success(page);
    }




}
